package com.fhzn.boss.web.request;

import com.fhzn.boss.entity.User;
import com.fhzn.boss.enums.CommonStatusEnum;
import com.fhzn.boss.enums.Constants;
import com.fhzn.boss.tools.AESEncryptorUtils;
import com.fhzn.commons.toolkit.exception.BuzException;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * @author 老顾
 * @apiNote
 * @since 2023/12/27
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class UserOpRequest extends BaseRequest {

    private Integer id;

    private String nickname;

    private String password;

    private String nicknameCn;

    private Integer status;

    private Integer userType;

    private String mobile;

    private String tel;

    private String station;


    /**
     * 角色列表,逗号隔开
     */
    private String roles;

    /**
     * 角色 code 列表，逗号隔开
     */
    private String roleCodes;

    /**
     * 用户所属系统应用
     */
    private String appCode;

    private String email;

    public void checkAdd() {
        checkCommon();
    }

    public void checkUpdate() {
        if (id == null) {
            throw new BuzException("id不能为空");
        }
        checkCommon();
    }


    public void checkCommon() {
        this.checkBaseParams();
        if (status == null || !CommonStatusEnum.contains(status)) {
            throw new BuzException("状态位不正确");
        }

    }


    /**
     * 转换为服务层参数
     *
     * @return 用户信息
     */
    @Override
    public User genServiceParam() {
        User user = new User();
        user.setId(this.getId());
        user.setNickname(this.getNickname());
        user.setNicknameCn(this.getNicknameCn());
        user.setEmail(this.getEmail());
        user.setModifier(this.getHandler());
        user.setMobile(this.getMobile());
        user.setTel(this.getTel());
        user.setStatus(this.getStatus());
        user.setPasswordExpired(Constants.PASSWORD_EXPIRED);
        user.setPassword(AESEncryptorUtils.encrypt(this.getPassword()));
        user.setAppCode(this.getAppCode());
        return user;
    }

    public User addGenServiceParam() {
        User user = genServiceParam();
        user.setCreator(this.getHandler());
        return user;
    }

    public User updateGenServiceParam() {
        User user = genServiceParam();
        user.setModifier(this.getHandler());
        return user;
    }

}
